﻿using B.MES.Customer.Domain;
using SqlSugar;
using System.Data;

namespace B.MES.Customer.Infrastructure
{
    /// <summary>
    /// 上下文
    /// </summary>
    public class DbcontextEcm
    {
       /// <summary>
       /// sqlsugar服务
       /// </summary>
        private readonly ISqlSugarClient db;
        /// <summary>
        /// 构造函数，用于初始化 DbcontextEcm 类的实例
        /// </summary>
        /// <param name="db">sqlsugar服务</param>
        public DbcontextEcm(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 创建数据表格
        /// </summary>
        public void CreateTable()
        {
            // 创建数据库
            db.DbMaintenance.CreateDatabase();
            //设定字符串默认长度为50，备份表格，并初始化数据库表格
            db.CodeFirst.SetStringDefaultLength(50).BackupTable().InitTables(new Type[]
            {
                typeof(AdditionalExpenditure),
                typeof(BomTab),
                typeof(ArtifactType),
                typeof(Client),
                typeof(ExpenditureType),
                typeof(IncomeStatement),
                typeof(IncomeType),
                typeof(Material),
                typeof(OutTab),
                typeof(OutWarehouse),
                typeof(Permission),
                typeof(Produce),
                typeof(Product),
                typeof(Product_Category),
                typeof(ProductAttributes),
                typeof(Role),
                typeof(Role_Permission),
                typeof(SalesManagement),
                typeof(StockIn),
                typeof(Tickets),
                typeof(Unit),
                typeof(User),
                typeof(User_Role),
                typeof(Warehouse),
                typeof(MatUnitInfo),
                typeof(Matattribute),
                typeof(MatTypeinfo),
                typeof(sale_Product),
            });
        }
        /// <summary>
        /// 创建类文件
        /// </summary>
        public void CreateClassFile()
        {
            //创建带有属性的类文件到指定路径
            db.DbFirst.IsCreateAttribute().CreateClassFile("D:\\Models");
        }
    }
}
